package com.nopay.cdxphone.ui.fragment.task;

import android.annotation.SuppressLint;
import android.os.Handler;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.nopay.cdxphone.base.BaseFragment;
import com.nopay.cdxphone.bean.AreaBean;
import com.nopay.cdxphone.bean.CommonResponse;
import com.nopay.cdxphone.bean.request.DeleteCallTaskFromGroupRequest;
import com.nopay.cdxphone.bean.request.GetCallTaskUnCallRequest;
import com.nopay.cdxphone.bean.request.SaveCallDataRequest;
import com.nopay.cdxphone.bean.response.AreaInfoResponse;
import com.nopay.cdxphone.bean.response.GetCallTaskCallResponse;
import com.nopay.cdxphone.bean.response.SaveCallDataResponse;
import com.nopay.cdxphone.dialog.AreaPopup;
import com.nopay.cdxphone.dialog.DialogCallDelaySetting;
import com.nopay.cdxphone.dialog.DialogDelayForAutoCall;
import com.nopay.cdxphone.dialog.DialogDeleteCallTask;
import com.nopay.cdxphone.dialog.DialogLoading;
import com.nopay.cdxphone.dialog.DialogPleaseOpenCallSetting;
import com.nopay.cdxphone.dialog.DialogPleaseOpenMember;
import com.nopay.cdxphone.dialog.DialogSubmitError;
import com.nopay.cdxphone.dialog.DialogWaitCallTransferSetting;
import com.nopay.cdxphone.receiver.PhoneBroadcastReceiver;
import com.nopay.cdxphone.utils.CallLogHelper;
import com.nopay.cdxphone.utils.CallTransferUtil;
import com.nopay.cdxphone.utils.MemberManager;
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter;
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout;
import com.lcodecore.tkrefreshlayout.header.SinaRefreshView;

import org.jetbrains.annotations.NotNull;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import com.nopay.cdxphone.Constants;
import com.nopay.cdxphone.R;
import com.nopay.cdxphone.databinding.FragmentToBeDialedBinding;

import com.nopay.cdxphone.ui.adapter.ToBeDialedAdapter;
import com.nopay.cdxphone.ui.ativity.CallTaskDetailActivity;

import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ToBeDialedFragment extends BaseFragment<FragmentToBeDialedBinding> {

    private CallTaskDetailActivity getParentActivity() {
        return ((CallTaskDetailActivity) getActivity());
    }

    private int getCallTaskId() {
        if(getParentActivity() == null) {
            return -1;
        }
        return getParentActivity().mCallTask.getId();
    }

    private List<GetCallTaskCallResponse.Data> dataList = new ArrayList<>();
    private int page = 1;
    private ToBeDialedAdapter adapter;
    private AreaPopup areaPopup;
    private List<AreaInfoResponse.Data> conditionList;

    private boolean isAuto = false;
    @Override
    protected void loadData() {
        //添加头
        SinaRefreshView headerView = new SinaRefreshView(getActivity());
        headerView.setArrowResource(R.mipmap.arrow);
        headerView.setTextColor(0xff745D5C);
        binding.refreshLayout.setHeaderView(headerView);
        //添加底
        com.lcodecore.tkrefreshlayout.footer.LoadingView loadingView = new com.lcodecore.tkrefreshlayout.footer.LoadingView(getActivity());
        binding.refreshLayout.setBottomView(loadingView);

        binding.rvCall.setLayoutManager(new LinearLayoutManager(getActivity()) {
            @Override
            public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
                try {
                    super.onLayoutChildren(recycler, state);
                } catch (IndexOutOfBoundsException e) {
                    e.printStackTrace();
                }
            }
        });
        adapter = new ToBeDialedAdapter(dataList);
        adapter.setOnItemChildClickListener((baseAdapter, view, position) -> {
            // TODO 点击进行拨号
            if(!MemberManager.hasMemberPermission()) {
                if(!MemberManager.hasFreeTimes()) {
                    ToastUtils.showShort("今日免费次数已用完");

//                    new DialogPleaseOpenMember(getActivity()).setOnOpenMemberConfirmListener(() -> {
//                        ((CallTaskDetailActivity) getActivity()).openMemberPage();
//                    }).show();
                    return;
                }
            }

            if(!SPUtils.getInstance().getBoolean(Constants.SP_ENABLED_CALL_TRANSFER, false)) {
                new DialogPleaseOpenCallSetting(getActivity()).setOnOpenCallSettingConfirmListener(() -> {
                    // TODO 开启防封设置
                    ((CallTaskDetailActivity) getActivity()).openCallSetting();
                }).show();
                return;
            }

            MemberManager.addUseTimes();

            GetCallTaskCallResponse.Data bean = adapter.getItem(position);
            if(bean == null) return;

            if(CallTransferUtil.open(getActivity(), bean.getPhone())) {
                if(SPUtils.getInstance().getBoolean(Constants.SP_SIM_TELECOM, false)) {
                    initDianXinCallFinishListener(bean);
                } else {
                    initCallFinishListener(bean);
                    new DialogWaitCallTransferSetting(getActivity())
                            .setOnWaitFinishListener(() -> CallTransferUtil.call(getActivity(), ""))
                            .show(SPUtils.getInstance().getInt(Constants.SP_CALL_TRANSFER_TIME_OUT, Constants.CALL_TRANSFER_DEFAULT_TIME_OUT));
                }
            } else {
                initCallFinishListener(bean);
                CallTransferUtil.call(getActivity(), bean.getPhone());
            }
        });

        binding.rvCall.setAdapter(adapter);
        binding.refreshLayout.setOnRefreshListener(new RefreshListenerAdapter() {
            @Override
            public void onRefresh(final TwinklingRefreshLayout refreshLayout) {
                page = 1;
                dataList.clear();
                requestCallData(() -> {
                    // TODO 处理请求结果
                    refreshLayout.finishRefreshing();
                    adapter.notifyDataSetChanged();
                });
            }

            @Override
            public void onLoadMore(final TwinklingRefreshLayout refreshLayout) {
                requestCallData(() -> {
                    refreshLayout.finishLoadmore();
                    adapter.notifyDataSetChanged();
                });
            }
        });

        binding.btnAutoCall.setOnClickListener(v -> {
            // TODO 自动拨号
            isAuto =  true;
            startAutoCall();
        });

        int autoCallDelay = SPUtils.getInstance().getInt(Constants.SP_AUTO_CALL_DELAY, 5);
        binding.btnAutoCallDelay.setText(autoCallDelay + "秒");
        binding.btnAutoCallDelay.setOnClickListener(v -> {
            // TODO 弹出自动延时时间弹框
            new DialogCallDelaySetting(getActivity()).setOnSelectedItemListener(delay -> {
                SPUtils.getInstance().put(Constants.SP_AUTO_CALL_DELAY, delay);
                binding.btnAutoCallDelay.setText(delay + "秒");
//                if(delay == 5) {
//                    new DialogCallDelayTips(getActivity()).show();
//                }
            }).show();
        });

        binding.btnClearTask.setOnClickListener(v -> {
            // TODO 清空当前页面数据
            if(dataList.size() == 0) {
                ToastUtils.showShort("当前无待拨打任务");
                return;
            }

            new DialogDeleteCallTask(getActivity()).setOnDeleteCallTaskConfirmListener(() -> {
                DeleteCallTaskFromGroupRequest bean = new DeleteCallTaskFromGroupRequest();
                bean.setPhone(SPUtils.getInstance().getString(Constants.SP_PHONE, ""));
                bean.setTaskId(getCallTaskId());
                bean.setType(2);

                RequestBody body = getRequestBody(bean);
                api.doDeleteCallTaskFromGroup(body).enqueue(new Callback<CommonResponse>() {
                    @Override
                    public void onResponse(Call<CommonResponse> call, Response<CommonResponse> response) {
                        if (response.body() != null) {
                            if (Constants.RESULT_OK.equals(response.body().getCode())) {
                                ToastUtils.showShort("删除成功");
                                ((CallTaskDetailActivity) getActivity()).initCallCount();
                                binding.refreshLayout.startRefresh();
                            } else {
                                ToastUtils.showShort(response.body().getMsg());
                            }
                        }
                    }

                    @Override
                    public void onFailure(Call<CommonResponse> call, Throwable t) {
                        ToastUtils.showShort(getActivity().getResources().getString(R.string.call_api_error));
                    }
                });
            }).show("确定要删除所有待拨打任务吗？");
        });

        binding.tvArea.setOnClickListener(view -> {
            // TODO 区域筛选
            if(areaPopup == null)
                areaPopup = new AreaPopup(getActivity())
                        .setAnchorView(view.getRootView())
                        .setData(areaBeans)
                        .setOnAreaConfirmListener(datas -> {
                            // TODO 重置筛选条件
                            conditionList = datas;
                            binding.refreshLayout.startRefresh();
                        })
                        .apply();
            areaPopup.showAtAnchorView();
        });

        binding.refreshLayout.startRefresh();

        api.doFindAreaInfo(getCallTaskId()).enqueue(new Callback<AreaInfoResponse>() {
            @Override
            public void onResponse(Call<AreaInfoResponse> call, Response<AreaInfoResponse> response) {
                if(response.body() != null) {
                    if(Constants.RESULT_OK.equals(response.body().getCode())) {
                        sortAreaData(response.body().getData());
                    }
                }
            }

            @Override
            public void onFailure(Call<AreaInfoResponse> call, Throwable t) {

            }
        });

        getCallLog(0);
    }

    private void requestCallData(OnRequestDataCompleteCallback callback) {
        String phone = SPUtils.getInstance().getString(Constants.SP_PHONE, "");
        Integer taskId = getCallTaskId();

        GetCallTaskUnCallRequest request = new GetCallTaskUnCallRequest();
        request.setPhone(phone);
        request.setTaskId(taskId);
        request.setAreaS(conditionList);
        request.setPageNum(page);
        request.setPageSize(50);

        RequestBody body = getRequestBody(request);
        api.doGetCallTaskUnCall(body).enqueue(new Callback<GetCallTaskCallResponse>() {
            @Override
            public void onResponse(Call<GetCallTaskCallResponse> call, Response<GetCallTaskCallResponse> response) {
                if (response.body() != null) {
                    if (Constants.RESULT_OK.equals(response.body().getCode())) {
                        List<GetCallTaskCallResponse.Data> callInfoList = response.body().getRows();
                        if(callInfoList != null && callInfoList.size() > 0) {
                            dataList.addAll(callInfoList);
                            page++;
                        }
                    } else {
                        ToastUtils.showShort(response.body().getMsg());
                    }
                }

                if(callback != null) callback.onComplete();
            }

            @Override
            public void onFailure(Call<GetCallTaskCallResponse> call, Throwable t) {
                if(callback != null) callback.onComplete();
                ToastUtils.showShort(getActivity().getResources().getString(R.string.call_api_error));
            }
        });
    }

    public interface OnRequestDataCompleteCallback {

        void onComplete();
    }

    public void initDianXinCallFinishListener(GetCallTaskCallResponse.Data callInfo) {
        PhoneBroadcastReceiver.setOnCallFinishListener((status, callLogItem) -> {
            if(status == 1) {
                new Handler().postDelayed(() -> {
                    initCallFinishListener(callInfo);
                    CallTransferUtil.call(getActivity(), "");
                }, 100);
            }
        });
    }

    public void initDianXinCloseCallFinishListener() {
        PhoneBroadcastReceiver.setOnCallFinishListener((status, callLogItem) -> {
            if(status == 1) {
                new Handler().postDelayed(() -> {
                    if(getActivity() != null) {
                        new DialogDelayForAutoCall(getActivity())
                                .setOnDelayCompleteListener(() -> startAutoCall())
                                .setOnStopCallListener(() -> {
                                    isAuto = false;
                                    currentIndexForAutoCall = -1;
                                    binding.refreshLayout.startRefresh();
                                    ((CallTaskDetailActivity) getActivity()).initCallCount();
                                }).show(SPUtils.getInstance().getInt(Constants.SP_AUTO_CALL_DELAY, 5));
                    }
                }, 100);
            }
        });
    }

    public void initCallFinishListener(GetCallTaskCallResponse.Data callInfo) {
        PhoneBroadcastReceiver.setOnCallFinishListener((status, callLogItem) -> {
//            if(isAuto) {
//                dataList.get(currentIndexForAutoCall).setSelected(false);
//
//                // TODO 弹出自动拨号延迟对话框
//                if(getActivity() != null) {
//                    new DialogDelayForAutoCall(getActivity())
//                            .setOnDelayCompleteListener(this::startAutoCall)
//                            .setOnStopCallListener(() -> {
//                                binding.refreshLayout.startRefresh();
//                                ToastUtils.showShort("停止拨号");
//                                currentIndexForAutoCall = -1;
//                                isAuto = false;
//                                ((CallTaskDetailActivity) getActivity()).initCallCount();
//
//                                CallLogSaveTools.getInstance().hasDataAndSubmitDelayed(getParentActivity(), () -> {
//                                    binding.refreshLayout.startRefresh();
//                                    ((CallTaskDetailActivity) getActivity()).initCallCount();
//                                });
//                            }).show(SPUtils.getInstance().getInt(Constants.SP_AUTO_CALL_DELAY, 5));
//                }

//                String callingNumber = CallTransferUtil.getPhoneNumberByCallSetting();
//                @SuppressLint("SimpleDateFormat") String dateStr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
//                CallLogBean callLogBean = new CallLogSaveTools.Builder()
//                        .setCalledNumber(callInfo.getPhone())
//                        .setCallingNumber("".equals(callingNumber) ? "未知" : callingNumber)
//                        .setCompany(callInfo.getCompany())
//                        .setName(callInfo.getName())
//                        .setDuration(0)
//                        .setCallDate(dateStr)
//                        .setStatus(2)
//                        .setPhone(SPUtils.getInstance().getString(Constants.SP_PHONE, ""))
//                        .setTaskId(callInfo.getTaskId())
//                        .build();
//                CallLogSaveTools.getInstance().save(callInfo.getPhone(), callLogBean);
//            }

            if(status == 0) {
                if(getActivity() != null && !getActivity().isFinishing()) {
                    if(mLoading == null) mLoading = new DialogLoading(getActivity());
                    mLoading.show("正在保存通话记录...");
                }
            }

            if(status == 1) {
                if(callLogItem != null) {
                    // 保存通话记录
                    saveCallData(callInfo, callLogItem.getDate(), callLogItem.getDuration());
                } else {
                    // 保存通话记录
                    @SuppressLint("SimpleDateFormat") String dateStr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
                    saveCallData(callInfo, dateStr, 0);
                }

                if(!SPUtils.getInstance().getBoolean(Constants.SP_SIM_TELECOM, false)) {
                    boolean isOpen = SPUtils.getInstance().getBoolean(Constants.SP_ENABLED_CALL_TRANSFER, false);
                    if(isOpen && getActivity() != null && !getActivity().isFinishing()) CallTransferUtil.close(getActivity());
                }

//                new Handler().postDelayed(() -> {
//                    boolean isOpen = SPUtils.getInstance().getBoolean(Constants.SP_ENABLED_CALL_TRANSFER, false);
//                    if(isOpen) {
//                        if(isAuto && SPUtils.getInstance().getBoolean(Constants.SP_SIM_TELECOM, false)) initDianXinCloseCallFinishListener();
//                        if(getActivity() != null && !getActivity().isFinishing()) CallTransferUtil.close(getActivity());
//                    }
//                }, 100);
            }

//            if(mLoading == null) mLoading = new DialogLoading(getActivity());
//            mLoading.show("正在保存通话记录...");
//
//            new Thread(() -> {
//                CallLogHelper.CallLogItem callLogItem = getCallLog(5);
//                if(callLogItem != null) {
////                    if(isAuto) CallLogSaveTools.getInstance().update(callInfo.getPhone(), callLogItem.getDate(), callLogItem.getDuration());
//                    saveCallData(callInfo, callLogItem.getDate(), callLogItem.getDuration());
//                } else {
//                    @SuppressLint("SimpleDateFormat") String dateStr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
//                    saveCallData(callInfo, dateStr, 0);
//                }
//
//                boolean isOpen = SPUtils.getInstance().getBoolean(Constants.SP_ENABLED_CALL_TRANSFER, false);
//                if(isOpen) CallTransferUtil.close(getActivity());
//            }).start();
        });
    }

    private CallLogHelper.CallLogItem getCallLog(int times) {
        CallLogHelper.CallLogItem callLogItem = CallLogHelper.fetchNewCallLog(getActivity());
        if(callLogItem == null) {
            if(times > 0) {
                times--;
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                return getCallLog(times);
            }
        }
        return callLogItem;
    }

    private int currentIndexForAutoCall = -1;

    // 自动拨号
    public void startAutoCall() {
        if(!MemberManager.hasMemberPermission()) {
            if(!MemberManager.hasFreeTimes()) {
                ToastUtils.showShort("今日免费次数已用完");

                currentIndexForAutoCall = -1;
                isAuto = false;
//                new DialogPleaseOpenMember(getActivity()).setOnOpenMemberConfirmListener(() -> {
//                    ((CallTaskDetailActivity) getActivity()).openMemberPage();
//                }).show();
                return;
            }
        }

        if(!SPUtils.getInstance().getBoolean(Constants.SP_ENABLED_CALL_TRANSFER, false)) {
            currentIndexForAutoCall = -1;
            isAuto = false;

            new DialogPleaseOpenCallSetting(getActivity()).setOnOpenCallSettingConfirmListener(() -> {
                // TODO 开启防封设置
                ((CallTaskDetailActivity) getActivity()).openCallSetting();
            }).show();
            return;
        }

        MemberManager.addUseTimes();

        currentIndexForAutoCall++;

        if(dataList.size() <= currentIndexForAutoCall) {
            ToastUtils.showShort("无可拨打号码");
            currentIndexForAutoCall = -1;
            isAuto = false;
            binding.refreshLayout.startRefresh();
            ((CallTaskDetailActivity) getActivity()).initCallCount();

//            CallLogSaveTools.getInstance().hasDataAndSubmit(getParentActivity(), () -> {
//                binding.refreshLayout.startRefresh();
//                ((CallTaskDetailActivity) getActivity()).initCallCount();
//            });
            return;
        }

        dataList.get(currentIndexForAutoCall).setSelected(true);
        adapter.notifyDataSetChanged();
        GetCallTaskCallResponse.Data bean = dataList.get(currentIndexForAutoCall);
//        initCallFinishListener(bean);
//
//        if(CallTransferUtil.open(getActivity(), bean.getPhone())) {
//            new DialogWaitCallTransferSetting(getActivity())
//                    .setOnWaitFinishListener(() -> CallTransferUtil.call(getActivity(), ""))
//                    .show(SPUtils.getInstance().getInt(Constants.SP_CALL_TRANSFER_TIME_OUT, Constants.CALL_TRANSFER_DEFAULT_TIME_OUT));
//        } else {
//            CallTransferUtil.call(getActivity(), bean.getPhone());
//        }

        if(CallTransferUtil.open(getActivity(), bean.getPhone())) {
            if(SPUtils.getInstance().getBoolean(Constants.SP_SIM_TELECOM, false)) {
                initDianXinCallFinishListener(bean);
            } else {
                initCallFinishListener(bean);
                new DialogWaitCallTransferSetting(getActivity())
                        .setOnWaitFinishListener(() -> CallTransferUtil.call(getActivity(), ""))
                        .show(SPUtils.getInstance().getInt(Constants.SP_CALL_TRANSFER_TIME_OUT, Constants.CALL_TRANSFER_DEFAULT_TIME_OUT));
            }
        } else {
            initCallFinishListener(bean);
            CallTransferUtil.call(getActivity(), bean.getPhone());
        }
    }

    private List<AreaBean> areaBeans = null;

    private void sortAreaData(List<AreaInfoResponse.Data> datas) {
        if(datas == null || datas.size() == 0) return;

        Map<String, List<AreaBean.City>> dataMap = new LinkedHashMap<>();
        for (int i = 0; i < datas.size(); i++) {
            List<AreaBean.City> cityList = new ArrayList<>();
            if(datas.get(i) == null) {
                if(!dataMap.containsKey("未知")) {
                    cityList.add(new AreaBean.City("未知"));
                    dataMap.put("未知", cityList);
                }
            } else {
                if(dataMap.containsKey(datas.get(i).getProvince())) {
                    cityList = dataMap.get(datas.get(i).getProvince());
                }
                cityList.add(new AreaBean.City(datas.get(i).getCity()));
                dataMap.put(datas.get(i).getProvince(), cityList);
            }
        }

        for (Map.Entry<String, List<AreaBean.City>> entry : dataMap.entrySet()) {
            AreaBean areaBean = new AreaBean();
            areaBean.setProvince(entry.getKey());
            areaBean.setCityList(entry.getValue());
            if(areaBeans == null) areaBeans = new ArrayList<>();
            areaBeans.add(areaBean);
        }
    }

    private DialogLoading mLoading;

    /**
     * 保存通话记录
     * @param callInfo
     * @param dateStr
     * @param duration
     */
    public void saveCallData(GetCallTaskCallResponse.Data callInfo, String dateStr, int duration) {
        SaveCallDataRequest bean = new SaveCallDataRequest();
        bean.setCallDate(dateStr);
        bean.setCalledNumber(callInfo.getPhone());
        String callingNumber = CallTransferUtil.getPhoneNumberByCallSetting();
        bean.setCallingNumber("".equals(callingNumber) ? "未知" : callingNumber);
        bean.setCompany(callInfo.getCompany());
        bean.setDuration(duration);
        bean.setName(callInfo.getName());
        bean.setPhone(SPUtils.getInstance().getString(Constants.SP_PHONE, ""));
        bean.setStatus(duration > 0 ? 1 : 2);
        bean.setTaskId(callInfo.getTaskId());

        RequestBody body = getRequestBody(bean);
        api.doSaveCallData(body).enqueue(new Callback<SaveCallDataResponse>() {
            @Override
            public void onResponse(@NotNull Call<SaveCallDataResponse> call, @NotNull Response<SaveCallDataResponse> response) {
                if(mLoading != null) {
                    mLoading.dismiss();
                    mLoading = null;
                }

                if (response.body() != null) {
                    if (Constants.RESULT_OK.equals(response.body().getCode())) {
                        ToastUtils.showShort("通话记录已保存");
//                        CallLogSaveTools.getInstance().delete(callInfo.getPhone());

                        if(isAuto) {
                            dataList.get(currentIndexForAutoCall).setSelected(false);

                            if(getActivity() != null) {
                                new DialogDelayForAutoCall(getActivity())
                                        .setOnDelayCompleteListener(() -> startAutoCall())
                                        .setOnStopCallListener(() -> {
                                            isAuto = false;
                                            currentIndexForAutoCall = -1;
                                            binding.refreshLayout.startRefresh();
                                            ((CallTaskDetailActivity) getActivity()).initCallCount();
                                        }).show(SPUtils.getInstance().getInt(Constants.SP_AUTO_CALL_DELAY, 5));
                            }
                        } else {
                            binding.refreshLayout.startRefresh();
                            ((CallTaskDetailActivity) getActivity()).initCallCount();
                        }
                        return;
                    }
                }

                new DialogSubmitError(getActivity())
                        .setOnSubmitAgainListener(isAgain -> {
                            // TODO 再次保存
                            if(isAgain) {
                                saveCallData(callInfo, dateStr, duration);
                            } else {
                                if(isAuto) {
                                    isAuto = false;
                                    currentIndexForAutoCall = -1;
                                    binding.refreshLayout.startRefresh();
                                    ((CallTaskDetailActivity) getActivity()).initCallCount();
                                }
                            }
                        }).show();
            }

            @Override
            public void onFailure(@NotNull Call<SaveCallDataResponse> call, @NotNull Throwable t) {
                if(mLoading != null) {
                    mLoading.dismiss();
                    mLoading = null;
                }

                new DialogSubmitError(getActivity())
                        .setOnSubmitAgainListener(isAgain -> {
                            // TODO 再次保存
                            if(isAgain) {
                                saveCallData(callInfo, dateStr, duration);
                            } else {
                                if(isAuto) {
                                    isAuto = false;
                                    currentIndexForAutoCall = -1;
                                    binding.refreshLayout.startRefresh();
                                    ((CallTaskDetailActivity) getActivity()).initCallCount();
                                }
                            }
                        }).show();
            }
        });
    }
}
